package org.example.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName HelloJob
 * @Description TODO
 * @Author kiku
 * @Date 2024/8/31 16:18
 * @Version 1.0
 */
@Component
public class HelloJob {

    @XxlJob("demoJobHandler")
    public void helloJob(){
        System.out.println("hello xxl-job");
    }

    /**
     * 分片执行器
     */
    @XxlJob("shardingJobHandler")
    public void shardingJob(){
        //分片的参数获取
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        List<Integer> list = getList();
        for (Integer integer : list) {
            if (integer%shardTotal==shardIndex){
                System.out.println("当前第"+shardIndex+"分片执行了"+integer);
            }

        }
    }
    public List getList(){
        List<Integer> list=new ArrayList<>();
        for (int i = 0; i <10000 ; i++) {
            list.add(i);
        }
        return list;
    }
}
